package com.xhwl.data.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhwl.common.enums.config.IndustryEnum;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.Industry;
import com.xhwl.common.pojo.cent.sdata.ThemeConfig;
import com.xhwl.data.dao.IThemeConfigDao;
import com.xhwl.data.service.IIndustryService;
import com.xhwl.data.service.IThemeConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 业态个性化主题配置
 * @Author：lrp
 * @Description: 业态个性化主题配置
 * @Date: Create in 10:41 2021-10-30
 */
@RestController
public class ThemeConfigController {

    @Autowired
    private IThemeConfigService themeConfigService;

    @Autowired
    private IThemeConfigDao themeConfigDao;

    @Autowired
    private IIndustryService industryService;

    private static final Logger log = LoggerFactory.getLogger(AccountController.class);

    /**
     * 分页列表
     * @param page
     * @return
     */
    @RequestMapping(value = "/theme-config/page")
    public ResultJson page(Page page) {
        page.setRecords(OrderItem.descs("created_at"));
        Page<ThemeConfig> pageList = themeConfigService.page(page);
        List<ThemeConfig> records = pageList.getRecords();
        List<Industry> industryList = industryService.getAll();
        Map<Integer, String> map = new HashMap<>(industryList.size());
        for (Industry industry : industryList) {
            map.put(industry.getId(), industry.getName());
        }
        for (ThemeConfig record : records) {
            if (null != map.get(record.getIndustryId())) {
                record.setIndustryName(map.get(record.getIndustryId()));
            }
        }
        return ResultJson.success(pageList);
    }


    /**
     * 详情
     * @param industryId
     * @return
     */
    @GetMapping(value = "/theme-config/detail")
    public ResultJson detail(@RequestParam(value = "industryId", required = true) Short industryId) {
        QueryWrapper<ThemeConfig> themeConfQueryWrapper = new QueryWrapper<>();
        themeConfQueryWrapper.eq("industry_id", industryId);
        ThemeConfig themeConfig = themeConfigDao.selectOne(themeConfQueryWrapper);
        if (null != themeConfig) {
            Industry industry = industryService.getById(themeConfig.getIndustryId());
            if (null != industry) {
                themeConfig.setIndustryName(industry.getName());
                return ResultJson.success(themeConfig);
            }
        }
        return ResultJson.success();
    }


    /**
     * 新增
     * @param themeConfig
     * @return
     */
    @PostMapping(value = "/theme-config/create")
    public ResultJson create(@RequestBody ThemeConfig themeConfig) {
        ResultJson checkResult = check(themeConfig);
        if (!checkResult.getState()) {
            return checkResult;
        }
        // 检查平台信息
        Industry industry = industryService.getById(themeConfig.getIndustryId());
        if (null == industry) {
            return ResultJson.fail("参数错误，平台信息不存在");
        }
        // 检查平台主题是否已存在
        Boolean themeConfigFlag = themeConfigService.themeConfCheck(themeConfig);
        if (!themeConfigFlag) {
            return ResultJson.fail("该业态主题配置已存在");
        }
        themeConfigService.save(themeConfig);
        return ResultJson.success();
    }


    /**
     * 更新
     * @param themeConfig
     * @return
     */
    @PostMapping(value = "/theme-config/update")
    public ResultJson update(@RequestBody ThemeConfig themeConfig) {
        if (null == themeConfig.getId()) {
            return ResultJson.fail("参数错误");
        }
        ResultJson checkResult = check(themeConfig);
        if (!checkResult.getState()) {
            return checkResult;
        }
        if (null == themeConfig.getId()) {
            return ResultJson.fail("参数错误");
        }
        // 平台信息是否存在
        Industry industry = industryService.getById(themeConfig.getIndustryId());
        if (null == industry) {
            return ResultJson.fail("参数错误，平台信息不存在");
        }
        // 检查业态主题是否已存在
        Boolean themeConfigFlag = themeConfigService.themeConfCheck(themeConfig);
        if (themeConfigFlag) {
            return ResultJson.fail("该业态主题配置不存在");
        }
        themeConfigService.updateById(themeConfig);
        return ResultJson.success("更新成功");
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping(value = "/theme-config/delete")
    public ResultJson delete(@RequestParam(value = "id", required = true) Integer id) {
        ThemeConfig themeConfig = themeConfigDao.selectOne(new QueryWrapper<ThemeConfig>().eq("industry_id", id));
        if (null == themeConfig) {
            return ResultJson.fail("主题配置不存在，删除失败");
        }
        themeConfigDao.deleteById(id);
        return ResultJson.success();
    }


    private ResultJson check(ThemeConfig themeConfig) {
        if (null == themeConfig) {
            return ResultJson.fail("参数错误");
        }
        if (null == themeConfig.getIndustryId()) {
            return ResultJson.fail("参数错误");
        }
        if (null == themeConfig.getLogo()) {
            return ResultJson.fail("参数错误");
        }
        if (null == themeConfig.getIcoUrl()) {
            return ResultJson.fail("参数错误");
        }
        if (null ==  themeConfig.getBackgroundUrl()) {
            return ResultJson.fail("参数错误");
        }
        if (null == themeConfig.getThemeColor()) {
            return ResultJson.fail("参数错误");
        }
        return ResultJson.success("检查通过");
    }
}
